<template>
	<common-page title="注册新账号">
		<el-form ref="stepForm" :model="form" :rules="rules" :label-width="120">
			<el-form-item label="登录账号" prop="username">
				<el-input v-model="form.username" placeholder="请输入登录账号"></el-input>
				<div class="el-form-item-msg">登录账号将作为登录时的唯一凭证</div>
			</el-form-item>
			<el-form-item label="登录密码" prop="password">
				<el-input v-model="form.password" type="password" show-password placeholder="请输入登录密码"></el-input>
				<sc-password-strength v-model="form.password"></sc-password-strength>
				<div class="el-form-item-msg">请输入包含英文、数字的6位以上密码</div>
			</el-form-item>
			<el-form-item label="确认密码" prop="password2">
				<el-input v-model="form.password2" type="password" show-password placeholder="请再一次输入登录密码"></el-input>
			</el-form-item>
			<el-form-item label="邮箱" prop="email">
				<el-input v-model="form.email" placeholder="请输入邮箱地址"></el-input>
			</el-form-item>
			<!-- <el-form-item label="" prop="agree">
				<el-checkbox v-model="form.agree" label="">已阅读并同意</el-checkbox><span class="link" @click="showAgree=true">《平台服务协议》</span>
			</el-form-item> -->
			<el-form-item  class="loginBtn">
				<el-button type="primary" :loading="isSaveing"  @click="submit">立即注册</el-button>
			</el-form-item>
		</el-form>


		<!-- <el-dialog v-model="showAgree" title="平台服务协议" :width="800" destroy-on-close>
			平台服务协议
			<template #footer>
				<el-button @click="showAgree=false">取消</el-button>
				<el-button type="primary" @click="showAgree=false;form.agree=true;">我已阅读并同意</el-button>
			</template>
		</el-dialog> -->
	</common-page>
</template>

<script>
	import scPasswordStrength from '@/components/scPasswordStrength';
	import commonPage from './components/commonPage'

	export default {
		components: {
			commonPage,
			scPasswordStrength
		},
		data() {
			return {
				stepActive: 0,
				showAgree: false,
				isSaveing: false,
				form: {
					username: "",
					password: "",
					password2: "",
					email: ''

				},
				rules: {
					username: [
						{ required: true, message: '请输入账号名'}
					],
					password: [
						{ required: true, message: '请输入密码'},
						{min: 6, message: '长度需为6位',  trigger: 'blur'}
					],
					password2: [
						{ required: true, message: '请再次输入密码'},
						{min: 6, message: '长度需为6位',  trigger: 'blur'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.password) {
								callback(new Error('两次输入密码不一致'));
							}else{
								callback();
							}
						}}
					],
					email: [
						{ required: true, message: '请输入邮箱地址'},
							{
							type: "email",
							message: "请输入正确的邮箱地址",
							trigger: ["blur"]
						}
					],
					// agree: [
					// 	{validator: (rule, value, callback) => {
					// 		if (!value) {
					// 			callback(new Error('请阅读并同意协议'));
					// 		}else{
					// 			callback();
					// 		}
					// 	}}
					// ],
					// userName: [
					// 	{ required: true, message: '请输入真实姓名'}
					// ],
					

				}
			}
		},
		mounted() {

		},
		methods: {
			submit(){
				this.$refs.stepForm.validate(async (valid) => {
					if (valid) {
						let { password2, ...form } = this.form
						this.isSaveing = true;
						var result = await this.$API.auth.register.post(this.form).then(res => {
							if(res.message == '用户注册成功'){
								this.$message.success(res.message)
								setTimeout(() => {
									this.goLogin()
								},1000)
							}
						}).finally(() => {
							this.isSaveing = false;
						})
						
					}
				})
			},
			goLogin(){
				this.$router.push({
					path: '/login'
				})
			}
		}
	}
</script>

<style scoped>
.loginBtn {
	margin-top: 30px;
}

</style>
